﻿#include "VampireSpider.h"
#include "../../MirEnvir/Envir.h"
#include "../../MirEnvir/Map.h"
#include "../../../Shared/Enums.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../../Shared/ServerPackets.h"
#include "../../../Shared/Data/Stat.h"
#include "../PlayerObject.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    VampireSpider::VampireSpider(MonsterInfo *info) : MonsterObject(info)
    {
        ActionTime = getEnvir()->getTime() + 1000;
    }

    std::string VampireSpider::getName() const
    {
        return Master == nullptr ? Info->getGameName() : (Dead ? Info->getGameName() : StringHelper::formatSimple("{0}({1})", Info->getGameName(), Master->getName()));
    }

    void VampireSpider::setName(const std::string &value)
    {
        throw NotSupportedException();
    }

    void VampireSpider::Process()
    {
        if (!Dead && Summoned)
        {
            if (Master != nullptr)
            {
                bool selfDestruct = false;
                if (FindObject(Master->ObjectID, 15) == nullptr)
                {
                    selfDestruct = true;
                }
                if (Summoned && getEnvir()->getTime() > AliveTime)
                {
                    selfDestruct = true;
                }
                if (selfDestruct && Master != nullptr)
                {
                    Die();
                }
            }
        }

        MonsterObject::Process();
    }

    void VampireSpider::Process(DelayedAction *action)
    {
        switch (action->Type)
        {
            case DelayedType::Damage:
                CompleteAttack(action->Params);
                break;
            case DelayedType::RangeDamage:
                CompleteRangeAttack(action->Params);
                break;
            case DelayedType::Recall:
                PetRecall(std::any_cast<MapObject*>(action->Params[0]));
                break;
        }
    }

    void VampireSpider::PetRecall(MapObject *target)
    {
        if (target == nullptr)
        {
            return;
        }
        if (Master == nullptr)
        {
            return;
        }
        Teleport(Master->getCurrentMap(), target->getCurrentLocation());
    }

    void VampireSpider::ProcessAI()
    {
        if (Dead)
        {
            return;
        }

        ProcessSearch();
        //todo ProcessRoaming(); needs no master follow just target roaming
        ProcessTarget();
    }

    void VampireSpider::Die()
    {
        MonsterObject::Die();

        //Explosion
        for (int y = getCurrentLocation()->Y - 1; y <= getCurrentLocation()->Y + 1; y++)
        {
            if (y < 0)
            {
                continue;
            }
            if (y >= getCurrentMap()->Height)
            {
                break;
            }

            for (int x = getCurrentLocation()->X - 1; x <= getCurrentLocation()->X + 1; x++)
            {
                if (x < 0)
                {
                    continue;
                }
                if (x >= getCurrentMap()->Width)
                {
                    break;
                }

                Cell *cell = getCurrentMap()->GetCell(x, y);

                if (!cell->getValid() || cell->Objects.empty())
                {
                    continue;
                }

                for (int i = 0; i < cell->Objects.size(); i++)
                {
                    MapObject *target = cell->Objects[i];
                    switch (target->getRace())
                    {
                        case ObjectType::Monster:
                        case ObjectType::Player:
                        {
                            //Only targets
                            if (!target->IsAttackTarget(this) || target->Dead)
                            {
                                break;
                            }
                            int value = target->Attacked(this,10 * PetLevel,DefenceType::MACAgility);
                            if (value <= 0)
                            {
                                break;
                            }
                            if (Master != nullptr)
                            {
                                MasterVampire(value, target);
                            }
                            break;
                        }
                    }
                }
            }
        }
    }

    bool VampireSpider::InAttackRange()
    {
        if (getTarget()->getCurrentMap() != getCurrentMap())
        {
            return false;
        }
        if (getTarget()->getCurrentLocation() == getCurrentLocation())
        {
            return false;
        }

        int x = std::abs(getTarget()->getCurrentLocation()->X - getCurrentLocation()->X);
        int y = std::abs(getTarget()->getCurrentLocation()->Y - getCurrentLocation()->Y);

        if (x > 1 || y > 1)
        {
            return false;
        }
        return (x <= 1 && y <= 1) || (x == y || x % 2 == y % 2);
    }

    void VampireSpider::Attack()
    {
        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));
        S::ObjectAttack *tempVar = new S::ObjectAttack();
        tempVar->ObjectID = ObjectID;
        tempVar->Direction = getDirection();
        tempVar->Location = getCurrentLocation();
        Broadcast(tempVar);

        AttackLogic();

        ActionTime = getEnvir()->getTime() + 300;
        AttackTime = getEnvir()->getTime() + AttackSpeed;
        ShockTime = 0;

        if (getTarget()->Dead)
        {
            FindTarget();
        }

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
    }

    void VampireSpider::AttackLogic()
    {
        int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
        if (damage == 0)
        {
            return;
        }
        Point *target = Functions::PointMove(getCurrentLocation(), getDirection(), 1);


        int value;
        if (target == getTarget()->getCurrentLocation())
        {
            value = getTarget()->Attacked(this, damage, DefenceType::MACAgility);
            if (value > 0 && Master != nullptr)
            {
                MasterVampire(value, getTarget());
            }
        }
        else
        {
            if (!getCurrentMap()->ValidPoint(target))
            {
                return;
            }

            Cell *cell = getCurrentMap()->GetCell(target);
            if (cell->Objects.empty())
            {
                return;
            }

            for (int o = 0; o < cell->Objects.size(); o++)
            {
                MapObject *ob = cell->Objects[o];
                if (ob->getRace() == ObjectType::Monster || ob->getRace() == ObjectType::Player)
                {
                    if (!ob->IsAttackTarget(this))
                    {
                        continue;
                    }

                    value = ob->Attacked(this, damage, DefenceType::MACAgility);
                    if (value > 0 && Master != nullptr)
                    {
                        MasterVampire(value, ob);
                    }
                }
                else
                {
                    continue;
                }

                break;
            }
        }
    }

    void VampireSpider::MasterVampire(int value, MapObject *ob)
    {
        if (Master == nullptr)
        {
            return;
        }
        if (Master->VampAmount == 0)
        {
            (static_cast<PlayerObject*>(Master))->VampTime = getEnvir()->getTime() + 1000;
        }
        Master->VampAmount += static_cast<unsigned short>(value * (PetLevel + 1) * 0.25F);
        S::ObjectEffect *tempVar = new S::ObjectEffect();
        tempVar->ObjectID = ob->ObjectID;
        tempVar->Effect = SpellEffect::Bleeding;
        tempVar->EffectType = 0;
        ob->Broadcast(tempVar);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
    }

    void VampireSpider::Spawned()
    {
        MonsterObject::Spawned();
        Summoned = true;
    }

    Packet *VampireSpider::GetInfo()
    {
        S::ObjectMonster *tempVar = new S::ObjectMonster();
        tempVar->ObjectID = ObjectID;
        tempVar->Name = getName();
        tempVar->NameColour = NameColour;
        tempVar->Location = getCurrentLocation();
        tempVar->Image = Monster::VampireSpider;
        tempVar->Direction = getDirection();
        tempVar->Effect = Info->Effect;
        tempVar->AI = Info->AI;
        tempVar->Light = Info->Light;
        tempVar->Dead = Dead;
        tempVar->Skeleton = Harvested;
        tempVar->Poison = CurrentPoison;
        tempVar->Hidden = getHidden();
        tempVar->Extra = Summoned;

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was used in a 'return' or 'throw' statement.
        return tempVar;
    }
}
